package com.sjx.boundarycallback.viewmodel;

import android.app.Application;
import android.os.AsyncTask;
import android.service.autofill.UserData;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;

import com.sjx.boundarycallback.api.User;
import com.sjx.boundarycallback.paging.UserBoundaryCallback;
import com.sjx.boundarycallback.room.UserDataBase;

public class UserViewModel extends AndroidViewModel {

    public static final int PER_PAGE = 8;
    public LiveData<PagedList<User>> userPageList;

    public UserViewModel(@NonNull Application application) {
        super(application);
        UserDataBase dataBase = UserDataBase.getInstance(application);
        userPageList = (new LivePagedListBuilder<>(dataBase.userDao().getUserList(),
                UserViewModel.PER_PAGE
                )).setBoundaryCallback(new UserBoundaryCallback(application))
                .build();
    }

    /**
     * 刷新数据
     */
    public void refresh() {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                UserDataBase.getInstance(getApplication())
                        .userDao()
                        .clear();
            }
        });
    }


}
